package com.woniu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.client.AccountClient;
import com.woniu.commons.model.StateData;
import com.woniu.dao.mapper.YLeagueClassMapper;
import com.woniu.dao.mapper.YUserLeagueClassMarginMapper;
import com.woniu.model.dto.LeagueWorkforceDto;
import com.woniu.model.dto.UserLeagueClassMarginDto;
import com.woniu.model.form.AddLeagueClassMarginForm;
import com.woniu.model.model.YCoach;
import com.woniu.model.model.YLeagueClass;
import com.woniu.model.model.YLeagueWorkforce;
import com.woniu.model.model.YUserLeagueClassMargin;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.service.YUserLeagueClassMarginService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@Service
public class YUserLeagueClassMarginServiceImpl extends ServiceImpl<YUserLeagueClassMarginMapper, YUserLeagueClassMargin> implements YUserLeagueClassMarginService {

    @Resource
    private YLeagueClassMapper leagueClassMapper;
    @Resource
    private AccountClient accountClient;
    @Override
    public Integer addLeagueClassMargin(AddLeagueClassMarginForm form) {
        YUserLeagueClassMargin userLeagueClassMargin = baseMapper.selectOne(new QueryWrapper<YUserLeagueClassMargin>().eq("personal_class_id", form.getPersonalClassId()).eq("user_id", form.getUserId()));
        Integer i ;
        if (userLeagueClassMargin != null){
            userLeagueClassMargin.setMarginNum(userLeagueClassMargin.getMarginNum()+form.getMarginNum());
            userLeagueClassMargin.setOrderNum(form.getOrderNum());
            i = baseMapper.updateById(userLeagueClassMargin);
        }else {
            YUserLeagueClassMargin m = BeanUtil.toBean(form, YUserLeagueClassMargin.class);
            YLeagueClass leagueClass = leagueClassMapper.selectById(form.getPersonalClassId());
            m.setCoachId(leagueClass.getCoachId());
            i = baseMapper.insert(m);
        }

        return i;
    }

    @Override
    public YUserLeagueClassMargin loadLeagueClassMarginByOrderNum(String orderNum) {

        YUserLeagueClassMargin userLeagueClassMargin = baseMapper.selectOne(new QueryWrapper<YUserLeagueClassMargin>().eq("order_num", orderNum));

        return userLeagueClassMargin;
    }

    @Override
    public List<Integer> loadLeagueStudentIds(Integer coachId) {
        List<YUserLeagueClassMargin> margins = baseMapper.selectList(new QueryWrapper<YUserLeagueClassMargin>().eq("coach_id", coachId).ne("margin_num", 0));
        List<Integer> collect = margins.stream().map(o -> o.getUserId()).collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<UserLeagueClassMarginDto> loadLeagueClassByUserToken(Integer userId, String leagueClassName) {
        QueryWrapper<YLeagueClass> wrapper = new QueryWrapper<>();
        if (leagueClassName!=null){
            wrapper.like("league_class_name", leagueClassName);
        }
        List<YLeagueClass> leagueClassList = leagueClassMapper.selectList(wrapper.ne("league_class_state", StateData.LEAGUE_CLASS_STATE_DISABLED));
        Set<Integer> collect = leagueClassList.stream().map(o -> o.getLeagueClassId()).collect(Collectors.toSet());

        List<YUserLeagueClassMargin> margins = baseMapper.selectList(new QueryWrapper<YUserLeagueClassMargin>().eq("user_id", userId).in("personal_class_id", collect));

        List<UserLeagueClassMarginDto> dtos = new ArrayList<>();

        for (YUserLeagueClassMargin m:margins
             ) {
            UserLeagueClassMarginDto dto = new UserLeagueClassMarginDto();
            dto.setMargin(m);
            dto.setCoach(BeanUtil.toBean( accountClient.byId(m.getCoachId()).getData(), YCoach.class));
            dto.setLeagueClass(leagueClassMapper.selectOne(new QueryWrapper<YLeagueClass>().eq("league_class_id",m.getPersonalClassId())));
            dtos.add(dto);
        }

        return dtos;
    }
}
